<template>
  <div>
    <el-form
      class="mt20"
      :disabled="type === 'view' || from !== 'person'"
      :rules="rules"
      :model="form"
      ref="form"
      label-width="100px"
    >
      <el-card class="mt20 mb20" shadow="never">
        <div slot="header">
          <span>本人学习情况</span>
        </div>
        <el-form-item class="cancel-width-item" prop="learningSituation">
          <el-input type="textarea" :rows="3" placeholder="请输入内容" v-model="form.learningSituation"></el-input>
        </el-form-item>
      </el-card>
      <el-card class="mb20" shadow="never">
        <div slot="header">
          <span>自我评价</span>
        </div>
        <el-form-item class="cancel-width-item" prop="selfComment">
          <el-input type="textarea" :rows="3" placeholder="请输入内容" v-model="form.selfComment"></el-input>
        </el-form-item>

        <el-row>
          <el-col :span="3" class="tl-center">
            <el-form-item class="cancel-width-item">
              <el-button @click="handleGetTemplate">获取模板</el-button>
            </el-form-item>
          </el-col>
          <el-col :span="9" class="tl-center">
            <el-form-item label="自评等级：" prop="selfLevel">
              <el-select
                clearable
                v-model="form.selfLevel"
                :disabled="!form.selfScore"
                placeholder="请选择"
              >
                <el-option
                  :value="item.moralityLevelId"
                  :label="item.moralityLevelName"
                  v-for="(item,index) in levelArr"
                  :key="index"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="自评分数：" prop="selfScore">
              <el-input :readOnly="true" v-model="form.selfScore" placeholder="请输入自评分数" />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="考评时间：">{{ formatTime(new Date(), 'day') }}</el-form-item>
          </el-col>
        </el-row>
      </el-card>
    </el-form>
    <comment-modal @chooseTemplate="handleChooseTemplate" ref="commentModal" />
  </div>
</template>

<script>
import CommentModal from './CommentModal'
import { getSession, noMatchLevel } from '@/utils/util'
import { getLevel } from '@/api/morality'
export default {
  components: {
    CommentModal
  },
  data() {
    return {
      from: this.$route.params.from || getSession('createEvaluationFrom'),
      type: this.$route.params.type || getSession('createEvaluationType'),
      row: this.$route.params.row || getSession('createEvaluationRow'),
      form: {},
      rules: {
        selfLevel: [{ required: true, message: '请选择自评等级', trigger: 'blur' }],
        selfScore: [{ required: true, message: '请输入自评分数', trigger: 'blur' }],
        learningSituation: [{ required: true, message: '请输入本人学习情况', trigger: 'blur' }],
        selfComment: [{ required: true, message: '请输入自我评价', trigger: 'blur' }]
      },
      levelArr: [],
      mdl: {},
      grade: 0,
      saveLevel: []
    }
  },
  // created() {
  //   console.log('条件值', this.type, this.from)
  // },
  methods: {
    edit(obj) {
      this.form = Object.assign({}, obj)
    },
    // 传递过来的 table 数组算出总分数
    countGrade(arr, num) {
      this.levelArr = [...this.saveLevel]
      let grade = 0
      arr.map(v => {
        grade += Number(v)
      })
      let addGrade = 0
      this.form.map.evalItemVOS.map(v => {
        addGrade += v.rewardValue
      })

      this.grade = Number(grade) + Number(addGrade)
      this.$set(this.form, 'selfScore', this.grade)
      let arr1 = this.levelArr.filter(v => this.grade >= v.minScore && this.grade <= v.maxScore)
      let arr2 = []
      if (arr1.length > 0) {
        arr2 = [...arr1]
      } else {
        // 没匹配到的情况
        arr2 = noMatchLevel(this.saveLevel, this.grade)
      }

      this.$nextTick(() => {
        this.levelArr = [...arr2]
        this.$set(this.form, 'selfLevel', this.levelArr[0].moralityLevelId)
      })
    },
    // 父组件获取数据
    getData() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.$emit('postData', 2, this.form)
        } else {
          this.$emit('notSubmit', 2)
        }
      })
    },
    // 选择模板后传递过来的对象
    handleChooseTemplate(obj) {
      this.mdl = Object.assign({}, obj)
      this.$set(this.form, 'selfComment', obj.commentContent)
    },
    handleGetTemplate() {
      this.$refs.commentModal.show(1)
    }
  },
  mounted() {
    getLevel({ moralityPlanId: this.row.moralityPlanId }).then(res => {
      if (res.state === '000000') {
        this.levelArr = res.data
        this.saveLevel = [...res.data]
      }
    })
  }
}
</script>
